<?php
/**
 * View  LaFabbrica.org
 * 
 * @author     luigi.pastore (gigi@la-fabbrica.org)
 * @created    17-mag-2011
 * @package    LaFabbrica
 * @copyright  Copyright (c) 2011 Luigi Pastore
 */
?>
<div id="container" class="fancybox">
    <script type="text/javascript">
	//<![CDATA[
	
	$(document).ready(function() {

	    var validator = $("#locationForm").validate({
		invalidHandler: function(e, validator) {
		    var errors = validator.numberOfInvalids();
		    if (errors) {
			$("ul#errorBox").show();
		    } else {
			$("ul#errorBox").hide();
		    };
		},
	    
		// Assegna classe 'error' al <li> dell'elemento invalido
		errorPlacement: function(error, element) {
		    //console.log(element);
		    element.parent("span").parent("div").parent("li").addClass("error");
		    error.appendTo( element.parent("span").parent("div"));
		},
		submitHandler: function(form) {
		    //$('#salva').attr('disabled', 'disabled');
		    $.fancybox.showActivity();

		    $.ajax({
			type	: "POST",
			cache	: false,
			url		: $(form).attr('action'),
			data	: $(form).serializeArray(),
			success: function(data) {
			    $.fancybox(data,{
				'showCloseButton' : false,
				'onClosed' : function(){
				    $('#locations').pagination({
					'targetUrl' : '/admin/ajax/list/locationsmini',
					'orderField' : 'location_name',
					'orderDir' : 'asc',
					'selected' : '<?php echo $_SESSION['Live']['Info']['live_location_id'] ? $_SESSION['Live']['Info']['live_location_id'] : '' ?>'
				    },selectable // CallBack
				);
				}                                        
			    });
			}
		    });

		    return false;
		},	    
		errorElement: "p",
		groups: {
		    address: "location_street location_street location_street_number location_loc location_province_id location_cap"
		  },
		rules: {
		    location_name:{
			required: true
		    },
		    location_street: {
			required: true
		    },
		    location_city: {
			required: true
		    },
		    location_cap:{
			digits: true,
			rangelength: [5, 5]
		    }
		},
		messages: {
		    location_name: {
			required: "<?php echo ERR_EMPTY ?>"
		    },
		    location_street: {
			required: "<?php echo ERR_EMPTY ?>"
		    },
		    location_city: {
			required: "<?php echo ERR_EMPTY ?>"
		    },
		    location_cap: {
			digits : "<?php echo  sprintf( ERR_FORMAT, 'CAP' )?>",
			rangelength : "<?php echo  sprintf( ERR_FORMAT, 'CAP' )?>"
		    }
		},
		debug:true
	    });
	});
    
	$("#salva").click(function() {
	    validator.form();
	});
	
	$("#close-fancybox").click(function(){
	    $.fancybox.close();
	});

	//]]>
    </script>

    <div class="info">
	<h2>Inserisci Nuova Location</h2>
	<div>La Location verr&agrave; aggiunto automaticamente alla lista della pagina precedente</div>
	<div>I campi contrassegnati da <span class="req">*</span> sono obbligatori!</div>
    </div>

    <form class="fab fancybox" name="locationForm" id="locationForm" action="/admin/ajax/form/newlocation" method="post">
	<ul id="errorBox">
	    <li id="errorLi" style="<?php echo $this->errorMsg ? '' : 'display:none' ?>">
		<h3 id="errorMsgLbl">Si &egrave; verificato un problema durante l'invio del modulo.</h3>
		<p id="errorMsg">
			Di seguito trovi evidenziati gli errori riscontrati.
		</p>
	    </li>
	    <li class="<?php echo $this->errorMsg['location_name'] ? 'error' : ''?>">
	    <label class="desc <?php echo $this->errorMsg['location_name'] ? 'error' : ''?>">Nome Location<span class="req">*</span></label>
	    <div>
		<span>
		    <input id="location_name" name="location_name" class="field text medium" type="text" maxlength="255" value="<?php echo $this->data['location_name']?>" /> 
		</span>
	    </div>
	    <p class="error" style="<?php echo $this->errorMsg['location_name'] ? 'display:block' : ''?>"><?php echo $this->errorMsg['location_name']?></p>
	</li>
	
	<li class="complex <?php echo $this->errorMsg['location_address'] ? 'error' : ''?>">
	    <label class="desc <?php echo $this->errorMsg['location_address'] ? 'error' : ''?>">Indirizzo<span class="req">*</span></label>
	    <div>
		<span class="input">
		    <input class="field text addr" name="location_street" id="location_street" size="50" type="text" value="<?php echo $this->data['location_street']?>" />
		    <label>Via</label>
		</span>
		<span class="input">
		    <input class="field text addr" name="location_street_number" id="location_street_number" size="2" type="text" value="<?php echo $this->data['location_street_number']?>" />
		    <label>N&deg;</label>
		</span>
		<span class="input">
		    <input class="field text addr" name="location_loc" id="location_loc" size="50" type="text" value="<?php echo $this->data['location_loc']?>" />
		    <label>Localit&aacute;</label>
		</span>
		<span class="input">
		    <input class="field text addr" name="location_city" id="location_city" size="50" type="text" value="<?php echo $this->data['location_city']?>" />
		    <label>Citt&aacute;</label>
		</span>
		<span class="input">
		    <input class="field text addr" name="location_cap" id="location_cap" size="10" type="text" value="<?php echo $this->data['location_cap']?>" />
		    <label>CAP</label>
		</span>

		<span class="left">
		    <select class="field select addr" name="location_province_id" id="location_province_id">
			<?php foreach( $this->province AS $k => $provincia ): ?>
			<option value="<?php echo $k?>" <?php echo $this->data['location_province_id'] == $k ? 'selected="selected"' : '' ?> ><?php echo $provincia['province_long_name']?></option>
			<?php endforeach; ?>
		    </select>
		    <label>Provincia</label>
		</span>
		<p class="error" style="<?php echo $this->errorMsg['location_province_id'] ? 'display:block' : ''?>"><?php echo $this->errorMsg['location_province_id']?></p>
	    </div>
	</li>	

	</ul>
	<div id="formButtons">
	    <button id="salva" type="submit" name="salva" value="salva" class="button positive">
		<img src="/images/admin/tick.png" alt="" />Salva
	    </button>
	    <button id="close-fancybox" type="button" name="close-fancybox" value="annulla" class="button negative">
		<img src="/images/admin/cross.png" alt="" />Annulla
	    </button>	    
	</div>
    </form>
</div>

<img id="bottom" class="fancybox" src="/images/admin/bottom.png" alt="" />
